//
//  OrderDetailModel.swift
//  Employee
//
//  Created by swordjoy on 2017/4/5.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import Foundation
import SwiftyJSON

struct OrderDetailModel {
  
  // 是否有记录,有记录就返回2,没有就1
  var sectionCount: Int { return records.count > 0 ? 2 : 1 }
  
  var cellCount: Int {
    if images.count > 0 && !launcherName.isEmpty { return 5 }
    else if images.count == 0 && launcherName.isEmpty { return 2 }
    else { return 4 }
  }
    
    var thumbImageURLs: [String] {
        return images.map({ return $0.thumbImageURL })
    }
  
  var isImages: Bool { return images.count > 0 }
  var isRecords: Bool { return records.count > 0 }
  
  let id: String
  let priorityLevel: String
  let title: String
  let content: String
  let createTime: String
  let status: String
  let launcherName: String
  let launcherMobile: String
  let number: String
  let category: String
  let images: [SJImage]
  // 受理人id
  let staffId: String
  let records: [Record]
  let houseAddress: [String]
  let tags: [String]
    let orgId: String
    let isCanGetOrder: Bool
    
  
  init(json: JSON) {
    
    self.id = json["detail"]["id"].stringValue
    self.priorityLevel = json["detail"]["priorityLevel"].stringValue
    self.title = json["detail"]["title"].stringValue
    self.content = json["detail"]["content"].stringValue
    self.createTime = json["detail"]["createTime"].stringValue
    self.launcherName = json["detail"]["launcherName"].stringValue
    self.launcherMobile = json["detail"]["launcherMobile"].stringValue
    self.number = json["detail"]["number"].stringValue
    self.category = json["detail"]["categoryName"].stringValue
    self.status = json["detail"]["status"].stringValue
    self.staffId = json["detail"]["staffId"].stringValue
    self.orgId = json["detail"]["orgId"].stringValue
    self.isCanGetOrder = json["acceptView"].boolValue
    
    self.images = SJImage.array(from: json["detail"]["images"].arrayValue)
    self.records = Record.array(from: json["record"].arrayValue)
    
    
    self.houseAddress = json["houseAddress"].arrayValue.map({ return $0.stringValue })
    self.tags = json["tags"].arrayValue.map({ return $0.stringValue })
    
  }
  
}

struct Record {
  
  let createTime: String
  let content: String
  let headImage: String
  let images: [ImageObject]
  let staffName: String
  
  init(json: JSON) {
    
    self.createTime = json["createTime"].stringValue
    self.content = json["content"].stringValue
    self.headImage = json["headImage"].stringValue
    self.staffName = json["staffName"].stringValue
    
    if json["images"].arrayValue.isEmpty { images = [] }
    else { images = ImageObject.array(from: json["images"].arrayValue) }
    
  }
  
  static func array(from jsonArray: [JSON]) -> [Record] {
    return jsonArray.map({ (json) -> Record in
      Record(json: json)
    })
  }
  
  var isImage: Bool { return images.count > 0 }
  
}

struct ImageObject: ModelProtocol {
  
  let accessUrl: String
  let thumbImageURL: String
  
  init(json: JSON) {
    self.accessUrl = json["accessUrl"].stringValue
    self.thumbImageURL = json["thumbImageURL"].stringValue
  }
  
  static func array(from jsonArray: [JSON]) -> [ImageObject] {
    return jsonArray.map({ (json) -> ImageObject in ImageObject(json: json) })
  }
  
}















